/** 
  -- The Performance Driven Lime Project -- 

  A self adaptive tuple space for mobile agents and hosts by dynamic
  adapting tuple's replication policy.
  
  Copyright (C) 2008
  Xing Jiankuan, Pan Wenhui
  SAGroup of Tsinghua University

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  Lesser General Public License for more details.

  You should have received this copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
 */
package sagroup.pdlime;

import lights.interfaces.ITuple;
import lime2.LimeTemplate;
import lime2.Location;
import lime2.TupleID;

/**
 * A pattern for TVRTuple match. You can define a wildcard for specified
 * field's match. For compatibility, if you want to set the Location field
 * as a wildcard, use Location.UNSPECIFIED. For other fields, use <code>
 * null</code> directly.<br>
 * More details and examples about template can be found in {@link lime2.LimeTemplate}
 * 
 * @author Xing Jiankuan
 * 
 */
public class TVRLimeTemplate extends TVRLimeWrapper {

	/**
	 * for serialization
	 */
	private static final long serialVersionUID = -2129015036913161698L;

	/**
	 * Constructor method. Allows to perform searches using specific values;
	 * This constructor ignores LimeTemplate specified fields. They are all
	 * set unspecified implicitly. You can use another constructor for setting
	 * all fields.
	 * @author Xing Jiankuan
	 */
	public TVRLimeTemplate(TupleType type, TupleID versionLayerId, Long ver,
			Location masterCurr, Location masterDest, Boolean isReplica,
			ITuple t) {
		super(null, Location.UNSPECIFIED, Location.UNSPECIFIED, type,
			  versionLayerId, ver, masterCurr, masterDest, isReplica, t);
	}
	
	/**
	 * The template that can match any tuple
	 */
	public static final TVRLimeTemplate ANYTUPLE = new TVRLimeTemplate(null,
			Location.UNSPECIFIED, Location.UNSPECIFIED, null, null, null,
			Location.UNSPECIFIED, Location.UNSPECIFIED, null,
			TVRLimeConstants.NULLTUPLE);
	
	/**
	 * The template that can match any local tuple
	 */
	public static final TVRLimeTemplate LOCALTUPLE = new TVRLimeTemplate(null,
			Location.HERE, Location.HERE, null, null, null,
			Location.UNSPECIFIED, Location.UNSPECIFIED, null,
			TVRLimeConstants.NULLTUPLE);
	
	/**
	 * Constructor method. Allows to perform searches using specific values;
	 * You can set any fields of system defined fields here.
	 * @author Xing Jiankuan
	 */
	public TVRLimeTemplate(TupleID tid, Location curr, Location dest, 
						   TupleType type, TupleID vid, Long ver, 
						   Location masterCurr, Location masterDest,
						   Boolean isReplica, ITuple t) {
		super(tid, curr, dest, type, vid, ver, masterCurr, masterDest, isReplica, t);
	}
	
	/**
	 * Copy constructor
	 * @author Xing Jiankuan
	 */
	public TVRLimeTemplate(TVRLimeWrapper wrapper) {
		this(wrapper.getTID(), wrapper.getCurr(), wrapper.getDest(),
			 wrapper.getType(), wrapper.getVID(), wrapper.getVer(),
			 wrapper.getMasterCurr(), wrapper.getMasterDest(),
			 wrapper.isReplica, wrapper.getTuple());
	}

	/**
	 * toString method.
	 * @return concise textual representation of TVRLimeTemplate
	 */
	public String toString() {
		return "[TVRLimeTemplate] " + super.toString();
	}

	/**
	 * Convert current TVRLimeTemplate instance to LimeTemplate.
	 * All TVR related fields are flattened, and put into
	 * the result.
	 * @return a lime template instance
	 * @author Xing Jiankuan
	 */
	public LimeTemplate toLimeTemplate() {
		ITuple t = this.flattenTVRLimeWrapper(TVR_FIELDS);
		return new LimeTemplate(limeWrapper.cur, limeWrapper.dest, t);
	}
	
	/**
	 * Flatten this template
	 * @return flattened tuple
	 * @author Xing Jiankuan
	 */
	 public ITuple flattenTVRLimeTemplate() {
		 return this.flattenTVRLimeWrapper(ALL_FIELDS);
	 }
	 
	 /**
	  * Create a TVR template matching a specified version layer ID
	  * @param vid the version layer ID to be matched
	  * @return the constructed TVRLimeTemplate instance
	  * @author Xing Jiankuan
	  */
	 public static TVRLimeTemplate createVIDTemplate(TupleID vid) {
		 return new TVRLimeTemplate(null, Location.UNSPECIFIED, Location.UNSPECIFIED,
				 					null, vid, null, Location.UNSPECIFIED, Location.UNSPECIFIED,
				 					null, TVRLimeConstants.NULLTUPLE);
	 }
}